package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    //分页条件查询
    @Override

    public Page<Setmeal> findbyPage(Integer pageNum, Integer pageSize, String name) {
        //1构建套餐条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name), Setmeal::getName, name);
        //2构建分页
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //3查询分页
        page = setmealMapper.selectPage(page, qw);
        //4遍历套餐list集合
        List<Setmeal> setmealList = page.getRecords();
        for (Setmeal setmeal : setmealList) {
            //4-1查询口味
            // 4-2构建条件
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());
            //查询列表
            LambdaQueryWrapper<SetmealDish> dqw = new LambdaQueryWrapper<>();
            dqw.eq(SetmealDish::getDishId, setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishMapper.selectList(dqw);
            setmeal.setSetmealDishes(setmealDishList);

        }
        //返回分页对象
        return page;
    }

//回显数据
    @Override
    public List<Dish> findbyType(Long categoryId, String name) {
        //构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name), Dish::getName, name);
        qw.eq(categoryId!=null,Dish::getCategoryId, categoryId);
        qw.orderByAsc(Dish::getSort);
        //调用mapper
       return dishMapper.selectList(qw);
    }
    //新增套餐
    @Override
    public void save(Setmeal setmeal) {
    //直接保存菜品基本信息
        log.info("新增之前id：{}", setmeal.getId());
        setmealMapper.insert(setmeal);
        log.info("新增之后id：{}", setmeal.getId());
        //获取并遍历
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            //关联套餐id
            setmealDish.setSetmealId(setmeal.getId());
            //保存数据库
            setmealDishMapper.insert(setmealDish);
        }
    }

    //套餐回显
    @Override
    public Setmeal findById(Long id) {
        //先查询套餐的基本信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //2查询菜品列表
        //2-1构建条件
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);
        //执行查询
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);
        //将菜品设置到套餐中
        setmeal.setSetmealDishes(setmealDishes);
        //返回菜品
        return setmeal;
    }
//修改套餐
    @Override
    public void update(Setmeal setmeal) {
        //1先修改套餐基本信息
      setmealMapper.updateById(setmeal);

        //2-1先删除旧菜品
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmeal.getId());
        //2-2根据条件删除
        setmealDishMapper.delete(qw);
        //3遍历新菜品
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //3-1关联套餐id
            setmealDish.setSetmealId(setmeal.getId());
            //3-2保存数据库
            setmealDishMapper.insert(setmealDish);
        }
    }
//启停售卖
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
//构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids);//where id in (x,x,x)
        //构建修改实体
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        //执行数据库
        setmealMapper.update(setmeal, qw);
    }

    //套餐删除
    @Override
    public void delete(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids);
        qw.eq(Setmeal::getStatus, 1);
        Integer integer = setmealMapper.selectCount(qw);
        if (integer > 0) {
            throw new CustomException("删除的菜品状态必须为已停售！~~");
        }
        //删除套餐   delete from dish where id in (x,x,x)
        setmealMapper.deleteBatchIds(ids);
        //删除菜品 delete from dish_flavor where dish_flavor in (x,x,x)
        LambdaQueryWrapper<SetmealDish> qwe = new LambdaQueryWrapper<>();
        qwe.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(qwe);
    }
//根据分类id+ 状态 查询套餐列表
    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        //构建条件  select * from where  category_id = ? and status = ?
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId, categoryId);
        qw.eq(Setmeal::getStatus, status);
        //查询列表
        List<Setmeal> setmealList = setmealMapper.selectList(qw);
        //返回结果
        return setmealList;
    }
//查看套餐详情
    @Override
    public List<Dish> findDishDateil(Long id) {
        //1.查询套餐菜品中间表集合
        //1-1 构建条件 select * from setmeal_dish where setmeal_id =?
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);
        //1-2查询
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(qw);
        //遍历
        List<Dish> dishList = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishList) {
            //2-1查询菜品
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            //添加套餐中的数量
            dish.setCopies(setmealDish.getCopies());
            //返回菜品集合
            dishList.add(dish);
        }

        return dishList;
    }

}
